/**
 * Created by gongxun on 17/7/27.
 */

var PdkMyPlayerModel = function(){
    var that = new PdkPlayerModel();

    that.myInhandCardRectList = null;

    //当前选中的
    that.selectCurrentIndex = null;

    //第一次选中的
    that.selectBeganIndex = null;

    //选中准备变化的
    that.selectIndexs = null;

    //自己的手牌操作
    that.loadMyInhandCardAction = function(node){
        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: function(touch, event){
                var location = touch.getLocation();
                var point = mo.gameMainLayer.getTransfromPoint(location);

                return that.touchBegan(point);
            },
            onTouchMoved: function(touch, event){
                var location = touch.getLocation();
                var point = mo.gameMainLayer.getTransfromPoint(location);
                that.touchMoved(point);
            },
            onTouchEnded: function(touch, event){
                that.touchEnd();
            },
            onTouchCancelled : function(touch, event){
                that.touchCancel();
            }
        }, node);
    };

    that.touchBegan = function(point){
        // cc.log("began p x:" + point.x + " y:" + point.y);

        that.selectCurrentIndex = null;
        that.selectBeganIndex = null;
        that.selectIndexs = [];

        //载入所有牌的ui
        that.setInhandCardRectList();

        //设置触发
        that.onSelectPoint(point);

        if (that.selectCurrentIndex == null){
            that.touchCancel();
            return false;
        }else{
            return true;
        }
    };

    that.touchMoved = function(point){
        // cc.log("moved p x:" + point.x + " y:" + point.y);

        //设置触发
        that.onSelectPoint(point);
    };

    //选择结束
    that.touchEnd = function(){
        for (var i = 0; i < that.inhandCards.length; i++){
            that.inhandCards[i].setIsColorStats(false);
        }

        for (var i = 0; i < that.selectIndexs.length; i++){
            var index = that.selectIndexs[i];
            var card = that.inhandCards[index];
            card.setIsUpStats(!card.mIsUp);
        }

        //检测出牌选项
        mo.pdkTableStatus.checkSelectIsPlay();
    };

    //选择取消
    that.touchCancel = function(){
        that.selectCurrentIndex = null;
        that.selectIndexs = [];

        for (var i = 0; i < that.inhandCards.length; i++){
            that.inhandCards[i].setIsColorStats(false);
            that.inhandCards[i].setIsUpStats(false);
        }

        //通知ui重置
        mo.pdkTable.mPdkTableUILayer.mTuijianIndex = 0;

        //检测出牌选项
        mo.pdkTableStatus.checkSelectIsPlay();
    };

    //强制选中牌
    that.selectCardTuijian = function(cardNums){
        for (var i = 0; i < that.inhandCards.length; i++){
            that.inhandCards[i].setIsColorStats(false);
            that.inhandCards[i].setIsUpStats(false);
        }

        //找到index
        var selectIndexss = [];
        for (var i = 0; i < cardNums.length; i++){
            for (var j = 0; j < that.inhandCards.length; j++){
                if (that.inhandCards[j].mCardNum == cardNums[i]){
                    selectIndexss.push(j);
                    break;
                }
            }
        }

        that.selectIndexs = selectIndexss;

        for (var i = 0; i < that.selectIndexs.length; i++){
            var index = that.selectIndexs[i];
            var card = that.inhandCards[index];
            card.setIsUpStats(!card.mIsUp);
        }

        //检测出牌选项
        mo.pdkTableStatus.checkSelectIsPlay();
    };

    that.setInhandCardRectList = function(){
        that.myInhandCardRectList = [];
        var cardSize = mo.pdkCardHelper.getCardSize();

        for (var i = 0; i < that.inhandCards.length; i++){
            var point = mo.pdkCardHelper.getInhandCardPos(0, i, that.inhandCards.length);
            var rect = cc.rect();
            rect.x = point.x - cardSize.width / 2;
            rect.y = point.y - cardSize.height / 2;
            rect.width = cardSize.width;
            rect.height = cardSize.height;
            that.myInhandCardRectList.push(rect);

            // cc.log("rect x:" + rect.x + " y:" + rect.y + " w:" + rect.width + " h:" + rect.height);
        }
    };

    that.onSelectPoint = function(point){
        var selectIndex = null;
        for (var i = that.myInhandCardRectList.length - 1; i >= 0; i--){
            if (cc.rectContainsPoint(that.myInhandCardRectList[i], point)){
                selectIndex = i;
                break;
            }
        }

        // cc.log("selectIndex:" + selectIndex);
        //只计算selectIndex 到 selectBeganIndex之间的
        if (selectIndex != null && that.selectCurrentIndex != selectIndex){
            that.selectCurrentIndex = selectIndex;
            if (that.selectBeganIndex == null){
                that.selectBeganIndex = selectIndex;
            }
            //选牌音效
            mo.soundHelper.playPdkEffectSelectCard();

            var indexs = [];
            if (selectIndex < that.selectBeganIndex){
                for (var i = selectIndex; i <= that.selectBeganIndex; i++){
                    indexs.push(i);
                }
            }else{
                for (var i = that.selectBeganIndex; i <= selectIndex; i++){
                    indexs.push(i);
                }
            }

            that.selectIndexs = indexs;
            // cc.log("testtest len: " + indexs.length + " value:" + indexs.valueOf());

            for (var i = 0; i < that.inhandCards.length; i++){
                if (mo.gameHelper.findValueForArray(indexs, i) != null){
                    that.inhandCards[i].setIsColorStats(true);
                }else{
                    that.inhandCards[i].setIsColorStats(false);
                }
            }
        }


        // if (selectIndex != null && that.selectCurrentIndex != selectIndex){
        //     //如果在列表就移除 不在列表就加入
        //     var indexOf = that.selectIndexs.indexOf(selectIndex);
        //     if (indexOf < 0){
        //         that.selectIndexs.push(selectIndex);
        //         that.inhandCards[selectIndex].setIsColorStats(true);
        //         cc.log("color on");
        //     }else{
        //         that.selectIndexs.splice(indexOf, 1);
        //         that.inhandCards[selectIndex].setIsColorStats(false);
        //         cc.log("color off");
        //     }
        //
        //     that.selectCurrentIndex = selectIndex;
        // }
    };

    that.getUpCardNums = function () {
        var selectNums = [];
        for (var i = 0; i < that.inhandCards.length; i++){
            if (that.inhandCards[i].mIsUp){
                selectNums.push(that.inhandCards[i].mCardNum);
            }
        }
        return selectNums;
    };

    return that;
};